import LConf
import json
import os
import re
def reName(path,kvMap):
	fList = os.listdir(path)
	for one in fList:
		for item in kvMap:
			if '_2.' in one:
				tep = one.replace('_2.','.')
				if tep == item['k']:
					oPath = path + "\\" +one 
					nPath = path + "\\" + item['v']+'_2.mp4'
					os.rename(oPath,nPath)
					print("%s is ok"%one)
					break
			if one == item['k']:
				oPath = path + "\\" +one 
				nPath = path + "\\" + item['v']+'.mp4'
				os.rename(oPath,nPath)
				print("%s is ok"%one)
				break
			
def getKV(arrDat):
	kvMap = []
	for one in arrDat:
		temp = one['url'].split('/')
		k = temp[-1:][0]
		v = one['title']
		v = re.sub('[|/*~|"?! <>]','',v)
		v = v.replace('\\','')
		kvMap.append({"k":k,'v':v})
	return kvMap

def main():
	# vidoData = LConf.rJson('./vidoUrls.json')
	# kvMap = getKV(vidoData)
	# LConf.wJson('./reNameKV.json',kvMap)
	# 改名
	kvMap = LConf.rJson('./reNameKV.json')
	reName('E:\\application\\spyVido',kvMap)

if __name__ == '__main__':
	main()
